package org.weso.acota.enhacer;

import org.apache.log4j.Logger;
import org.weso.acota.dao.ontology.FileOntologyDAOImpl;
import org.weso.acota.exceptions.AcotaModelException;
import org.weso.acota.to.RequestSuggestionTO;
import org.weso.acota.to.SuggestionTO;

@SuppressWarnings("unused")
public abstract class EnhancerAdapter implements Enhancer {

	protected static Logger logger = Logger.getLogger(EnhancerAdapter.class);
	protected RequestSuggestionTO request;
	protected SuggestionTO suggest;
	protected Enhancer successor;
	
	@Override
	public SuggestionTO enhance(RequestSuggestionTO request) {
		try{
			this.request = request;
			preExecute();
			execute();
			postExecute();
			if(successor != null){
				return this.successor.enhance(this.request);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Exception enhancing request.", e);
			throw new AcotaModelException("Exception enhancing request.", e);
		}
		return  this.suggest;
	}
	
	public SuggestionTO getSuggest(){
		if(this.suggest==null){
			logger.debug("New instance singleton of suggestions");
			return suggest = new SuggestionTO();
		}else{
			logger.debug("Get instance singleton of suggestions");
			return this.suggest;
		}
	}

	@Override
	public void setSuccessor(Enhancer successor) {
		logger.debug("Set succesor of class");
		this.successor = successor;
	}
	
	protected abstract void execute() throws Exception;
	protected abstract void preExecute() throws Exception;
	protected abstract void postExecute() throws Exception;

}
